package io.github.wangtisnruipopo.carscore.types;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public abstract class AbsPlayer<D extends AbsDecorValue, V extends AbsCardValue, C extends AbsSimpleCard<D, V>> implements IPlayer<D, V, C> {
    @Getter
    @Setter
    protected String id;
    @Getter
    @Setter
    protected String name;
    @Getter
    protected List<C> cards;
    @Getter
    @Setter
    protected boolean computer;
    @Getter
    @Setter
    protected boolean alive;
    @Getter
    @Setter
    protected IGame<D,
            V,
            C,
            ? extends ISuitCards<D, V, C>,
            ? extends IPlayer<D, V, C>,
            ? extends IPlayCardsRecord<D, V, ? extends IPlayCardsType<C>, C, ? extends IPlayer<D, V, C>>,
            ?> game;
    @Setter
    protected IPlayEnd playEnd;
    @Setter
    protected IAppointPlay appointPlay;
    @Setter
    protected String licence;
    protected Timer timer;

    {
        this.cards = new ArrayList<>();
        this.computer = false;
        // 默认都是存活状态
        this.alive = true;
    }

    @Override
    public void push(C card) {
        this.cards.add(card);
    }

    @Override
    public void notifyPlay(long delay, IPlayCardsRecord<?, ?, ?, ?, ?> lastRecord) {
        if (this.isComputer()) {
            // AI走牌
            System.out.println("AI走牌了");
        } else {
            this.timer = new Timer();
            this.timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // 时间到，强制执行AI走牌逻辑
                    System.out.println("AI走牌了");
                    // 告诉回合对象，我走牌了
                }
            }, delay);
        }
    }

    @Override
    public void play() {
        this.timer.cancel();
        // 告诉回合者我走了什么牌
        this.appointPlay.addRecord(null, null, null, null, false);
        // 通知回合，我ok了
        this.playEnd.playEnd(this.licence);
    }

//    @Override
//    public void notifyPlay() {
//        // 判断是否是AI，是的话直接执行强制出牌逻辑，否则不用管，等待玩家出牌即可
//        // 创建定时器
//        this.timer = new Timer();
//        timer.schedule();
//    }

}
